package factories;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;


/**
 * Factory to construct grey scale / RGB buffers from image files
 * @author benjamin
 	This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
public class BufferFactory {
	/**
	 * Creates a grey scale buffer from the filename provided
	 * @param filename
	 * @return BufferedImage: a pixel buffer of the image
	 * @throws IOException if image cannot be loaded
	 */
	public static BufferedImage createBuffer(String filename) throws IOException{
		System.out.println(filename);
		BufferedImage imgColor = ImageIO.read(new File(filename));
		BufferedImage imgGrey = new BufferedImage(imgColor.getWidth(),imgColor.getHeight(),BufferedImage.TYPE_BYTE_GRAY);
		Graphics g = imgGrey.getGraphics();
		g.drawImage(imgColor,0,0,null);
		g.dispose();
		return imgGrey;
	}
	/**
	 * Constructs a colour image from grey scale pixel buffer
	 * @param imgGrey
	 * @return RGB pixel buffer
	 */
	public static BufferedImage createColourBufferFromGrey(BufferedImage imgGrey){
		BufferedImage imgColor = new BufferedImage(imgGrey.getWidth(), imgGrey.getHeight(), BufferedImage.TYPE_INT_RGB); 
		Graphics g = imgColor.getGraphics();
		g.drawImage(imgGrey, 0, 0, null);
		g.dispose();
		return imgColor;
	}
}
